package red.uec.util;

import java.util.Random;

public class RandomCodeUtil {
    // 验证码字符集
    private static final char[] CHAR_ARRAY = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};


    private static String getCode(int charCount, int offsetStart, int offsetEnd){
        Random random = new Random();
        StringBuilder stringBuffer = new StringBuilder(charCount);
        for (int i = 0; i < charCount; i++){
            stringBuffer.append(CHAR_ARRAY[offsetStart + random.nextInt(offsetEnd - offsetStart + 1)]);
        }
        return stringBuffer.toString();
    }

    public static String getCode(int count){
        return getCode(count, 0, CHAR_ARRAY.length - 1);
    }

    public static String getNumberCode(int count){
        return getCode(count, 0, 9);
    }

    public static String getLetterCode(int count){
        return getCode(count, 10, CHAR_ARRAY.length - 1);
    }

}
